package com.gzr.server.impl;

import com.gzr.mapper.UserMapper;
import com.gzr.pojo.User;
import com.gzr.server.UserServer;
import com.gzr.utils.Md5Util;
import com.gzr.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class UserServerImpl implements UserServer {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUserName(String username) {
        //查询用户
        User user = userMapper.findByUserName(username);
        return user;
    }

    @Override
    public void register(String username, String password) {
        String md5 = Md5Util.getMD5String(password);
        User userAdd = new User();
        userAdd.setUsername(username);
        userAdd.setPassword(md5);
        userMapper.add(userAdd);
    }

    @Override
    public void update(User user) {
        //当前时间
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        //获取用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        userMapper.updateAvatar(avatarUrl, id);
    }

    @Override
    public void updatePwd(String newPwd) {
        //获取用户id
        Map<String, Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd), id);
    }
}
